package com.li.controller.exception;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: rocketmq_boot
 * @description:
 * @author: li
 * @create: 2023-03-10 15:37
 **/
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public void handleCommonExp(Exception e) {
        logger.error("common exception handler  " + e.getMessage(), e);
        httpRequestLog();
    }

    public void httpRequestLog() {
        try {
            // 从RequestContextHolder 获取到 请求信息
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (requestAttributes != null) {
                HttpServletRequest request = requestAttributes.getRequest();
                logger.info("REQUEST URL={}  REQUEST Args={}", request.getRequestURL(), JSON.toJSONString(request.getParameterMap()));

            }
        } catch (Exception e) {

        }

    }
}
